package com.hexinfo.dmpro.common.aspect;

import com.hexinfo.dmpro.common.utils.SecurityUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Date;

/**
 * DAO切面，自动插入创建人，创建时间，修改人，修改时间
 * Created by JunjieM on 2020-3-12.
 */
@Aspect
@Component
@Configuration
public class DaoAspect {
    private static final String CREATE_BY = "createBy";
    private static final String CREATE_TIME = "createTime";
    private static final String LAST_UPDATE_BY = "lastUpdateBy";
    private static final String LAST_UPDATE_TIME = "lastUpdateTime";

    @Pointcut("execution(* com.hexinfo.dmpro.*.dao.*.update*(..))")
    public void daoUpdate() {
        // Do nothing
    }

    @Pointcut("execution(* com.hexinfo.dmpro.*.dao.*.insert*(..))")
    public void daoCreate() {
        // Do nothing
    }

    @Around("daoUpdate()")
    public Object doAroundUpdate(ProceedingJoinPoint pjp) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return pjp.proceed();
        }
        String username = null;
        try {
            username = SecurityUtils.getUsername();
        } catch (Exception e) {
            //
        }
        Object[] objs = pjp.getArgs();
        if (objs != null && objs.length > 0) {
            for (Object obj : objs) {
                if (username != null) {
                    BeanUtils.setProperty(obj, LAST_UPDATE_BY, username);
                }
                BeanUtils.setProperty(obj, LAST_UPDATE_TIME, new Date());
            }
        }

        return pjp.proceed();
    }

    @Around("daoCreate()")
    public Object doAroundCreate(ProceedingJoinPoint pjp) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return pjp.proceed();
        }
        String username = null;
        try {
            username = SecurityUtils.getUsername();
        } catch (Exception e) {
            //
        }
        Object[] objs = pjp.getArgs();
        if (objs != null && objs.length > 0) {
            for (Object obj : objs) {
                try {
                    if (username != null && StringUtils.isBlank(BeanUtils.getProperty(obj, CREATE_BY))) {
                        BeanUtils.setProperty(obj, CREATE_BY, username);
                    }
                    if (StringUtils.isBlank(BeanUtils.getProperty(obj, CREATE_TIME))) {
                        BeanUtils.setProperty(obj, CREATE_TIME, new Date());
                    }
                } catch (NoSuchMethodException e) {
                    //
                }
            }
        }
        return pjp.proceed();
    }
}
